package com.shop2cn.iapi.member.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.BindMobileBean;
import com.shop2cn.iapi.member.parameter.BriefMemberInfoResp;
import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.service.BindMobileCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.Member;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.utils.TestDataManager;
/**
*会员绑定手机号
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindMobile{
	private static BindMobileBean bindmobileBean;
	private static BindMobileCall bindmobileCall;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("会员绑定手机号");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		bindmobileBean=new BindMobileBean();
		bindmobileCall=new BindMobileCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindmobileCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMobile_001() {
		Logger.start(true,"会员绑定手机号成功");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("godmember", "bindmobile", bindmobileBean.getMobile());
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "验证消息");
//			{"csId":0,"mobile":"13032772594","requestId":"d06b9d729bc241e4b2fd159dc212ca93","shopId":500994922,"userId":500994921}
			Logger.verifyEquals(bindmobileBean.getShopId().toString(), joa.get(0).get("shopId").toString(), "验证shopId");
			Logger.verifyEquals(bindmobileBean.getMobile(), joa.get(0).get("mobile").toString(), "验证mobile");
			Logger.verifyEquals(bindmobileBean.getUserId().toString(), joa.get(0).get("userId").toString(), "验证userId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_002() {
		Logger.start(true,"不输入userid失败");
		try {
//			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("400", bindmobileCall.getString("code"), "验证code");
//			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_003() {
		Logger.start(true,"不输入shopid失败");
		try {
			bindmobileBean.setUserId(1);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
//			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("400", bindmobileCall.getString("code"), "验证code");
//			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_004() {
		Logger.start(true,"不输入mobile失败");
		try {
			bindmobileBean.setUserId(1);
//			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("400", bindmobileCall.getString("code"), "验证code");
//			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_005() {
		Logger.start(true,"userid和shopid不匹配失败");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			bindmobileBean.setUserId(userId+100);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("false", bindmobileCall.getString("data"), "验证data");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_006() {
		Logger.start(true,"会员已经绑定过手机覆盖");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_007() {
		Logger.start(true,"当前shop手机已经绑定过会员失败");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			//创建一个新会员
			UserCreateBean usercreateBean2 = DGUserCallService.initUsercreateBean();
			int userId2 = DGUserCallService.UserCreateCall(usercreateBean2);
			CreateMemberBean createMem2 = GodMemberService.initCreateMemberBean(shopid,userId2,usercreateBean2.getOpenId());
			createMem.setOpenId(createMem2.getOpenId());
			String memid2 = GodMemberService.creatememberCall(createMem2);
			
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			bindmobileBean.setUserId(userId2);
//			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("false", bindmobileCall.getString("data"), "验证data");
			Logger.verifyEquals(null, GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile没有绑定");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BindMobile_008() {
		Logger.start(true,"手机已经绑定过其他shop的会员,绑定当前shop可以成功");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			
			//第二家店
			Integer shopid2 = GodmchManageCallService.CreateSellerMch();
			createMem.setShopId(shopid2);
			String memid2 = GodMemberService.creatememberCall(createMem);
			bindmobileBean.setShopId(shopid2);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMobile_009() {
		Logger.start(true,"会员绑定手机号成功-邀请制传入csId");
		try {
			UserCreateBean u = DGUserCallService.initCsUsercreateBean();
			Integer csid = DGUserCallService.UserCreateCall(u);
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileBean.setCsId(csid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			BriefMemberInfoResp bm = GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId());
			Logger.verifyEquals(bindmobileBean.getMobile(), bm.getMobile(), "验证mobile");
			GodMemberWapper GodMemberWapper=new GodMemberWapper();
			Logger.verifyEquals(bindmobileBean.getCsId(), GodMemberWapper.selectMemberByMemberid(bm.getMemberId()).get(0).getCsId(), "验证mobile");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMobile_010() {
		Logger.start(true,"会员绑定手机号成功-存在分销员的情况绑定分销员");
		try {
			Integer shopId=DGUserCallService.TestWxsqOtherSellerId;
			String mobile=TestDataManager.getPhoneNum();
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			usercreateBean.setShopId(shopId);
			usercreateBean.setMobile(mobile);
			usercreateBean.setUserType(3);
			Integer userId = DGUserCallService.UserCreateCall(usercreateBean);
			UserCreateBean usercreateBean2 = DGUserCallService.initUsercreateBean();
			Integer userId2 = DGUserCallService.UserCreateCall(usercreateBean2);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopId,userId2,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			Integer csid=1;
//			GodMemberService.bindMobile(shopId, userId2, mobile,csid);
			
			bindmobileBean.setUserId(userId2);
			bindmobileBean.setMobile(mobile);
			bindmobileBean.setShopId(shopId);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			//验证BriefMemberInfo露出分销员id逻辑
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			Logger.verifyEquals(userId, GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getDsId(), "验证dsid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMobile_011() {
		Logger.start(true,"会员绑定手机号成功-多渠道");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			UserCreateBean usercreateBean2 = DGUserCallService.initUsercreateBean();
			usercreateBean2.setAppSource(DGUserCallService.getAppSourceCode(5));
			int userId2 = DGUserCallService.UserCreateCall(usercreateBean2);
			CreateMemberBean createMem2 = GodMemberService.initCreateMemberBean(shopid,userId2,usercreateBean2.getOpenId());
			createMem2.setAccessSource(5);
			createMem2.setOpenId(usercreateBean2.getOpenId());
			String memid2 = GodMemberService.creatememberCall(createMem2);
			
			bindmobileBean.setAccessSource(0);
			bindmobileBean.setUserId(userId);
			bindmobileBean.setMobile(TestDataManager.getPhoneNum());
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("godmember", "bindmobile", bindmobileBean.getMobile());
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "验证消息");
//			{"csId":0,"mobile":"13032772594","requestId":"d06b9d729bc241e4b2fd159dc212ca93","shopId":500994922,"userId":500994921}
			Logger.verifyEquals(bindmobileBean.getShopId().toString(), joa.get(0).get("shopId").toString(), "验证shopId");
			Logger.verifyEquals(bindmobileBean.getMobile(), joa.get(0).get("mobile").toString(), "验证mobile");
			Logger.verifyEquals(bindmobileBean.getUserId().toString(), joa.get(0).get("userId").toString(), "验证userId");
			//不同渠道AccessSource绑定一个手机号成功
			bindmobileBean.setAccessSource(5);
			bindmobileBean.setUserId(userId2);
			bindmobileBean.setShopId(shopid);
			bindmobileCall.setData(bindmobileBean);
			bindmobileCall.callService();			
			Logger.verifyEquals("200", bindmobileCall.getString("code"), "验证code");
			Logger.verifyEquals("true", bindmobileCall.getString("data"), "验证data");
			//手机号加密需要校验
			Logger.verifyEquals(bindmobileBean.getMobile(), GodMemberService.briefmemberinfoCall(bindmobileBean.getShopId(), bindmobileBean.getUserId()).getMobile(), "验证mobile");

		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}